//
//  MainViewController.m
//  PatientApp
//
//  Created by chirui on 17/5/2.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MainViewController.h"
#import "MainTableViewCell.h"
#import "PAdvertiseView.h"
#import "ADViewController.h"
#import "BigTurnTableViewController.h" // 大转盘
#import "MoreNewsViewController.h" // 大事件
#import "GoodsDetailsViewController.h"
#import "BrandTitleViewController.h" //品牌头条
#import "FlashTrainViewController.h" // 闪购车通车
#import "NowFishViewController.h" // 鲫时鱼

#import "CompetitionListViewController.h" //竞赛排名活动
#import "SalesRankViewController.h"  // 超值
#import "BagRankListViewController.h" // 大咖活动排名
#import "JuNengLolViewController.h" // 聚能联盟

@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
{
    
    UITableView * _tableView;
    float aotoHeight;
    UIView * alertBgView;
    
    NSArray * bannerDataArray;
    NSArray * newsGoodsarray;
    
    NSDictionary * dataDic;
    NSArray * newsArray;
}

@end

@implementation MainViewController


-(void)viewWillAppear:(BOOL)animated {
    
    NSString * userStr = USER_DEFAULTGet(@"user_id");
    
    if (userStr.length>0) {
        
        [self getUserMessageRequest];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TitleView(@"红福人家")
    
    [self creatTableView];
    
    [self getBannerNetWorking];
    [self getMainNewsGoodsRequest];
    [self getMainClassDataRequest];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64-48) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getBannerNetWorking];
        [self getMainNewsGoodsRequest];
        [self getMainClassDataRequest];

    }];
    _tableView.mj_header = header;
    
    
    [_tableView registerNib:[UINib nibWithNibName:[MainTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return newsArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    aotoHeight = (WIDTH -40)/3 * 1.25;
    
    return 1080+160+aotoHeight*4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = newsArray[indexPath.row];
    MainTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[MainTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    [cell.newsImage sd_setImageWithURL:ImageUrl(dic[@"img_src"]) placeholderImage:ImageName(@"img_new_default")];
    cell.newsTitle.text = dic[@"title"];
    cell.timeLabel.text = dic[@"create_time"];
    cell.newsDetails.text = dic[@"brief"];
    
    cell.newsDetails.frame = CGRectMake(125, 44, WIDTH-135, 20);
    cell.newsDetails.numberOfLines = 2;
    [cell.newsDetails sizeToFit];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = newsArray[indexPath.row];
    JuNengLolViewController * details = [[JuNengLolViewController alloc]init];
    details.newsIdStr = dic[@"news_id"];
    details.titleStr = @"大事件";
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
    
}

-(void)signBtnClick{
    
    if (verifTokenAndEnter) {
        
        [self siginScoreRequest];
    }else {
        
        LogingViewController * login =[[LogingViewController alloc]init];
        login.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:login animated:YES];
        
    }
    
}

//签到
-(void)signBtnClickForStr:(NSString *)score{
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    
    alertBgView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    alertBgView.backgroundColor = ColorA(0, 0, 0, 0.5);
    
    
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake((WIDTH - 265)/2, (HEIGHT-215)/2-40, 265, 215)];
    image.image = ImageName(@"img_window_bg");
    [alertBgView addSubview:image];
    
    
    UILabel * nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(30, 130, 205, 30)];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.textColor = RGB(0xf46048);
    nameLabel.text = score;
    [image addSubview:nameLabel];
    
    UIButton * deledateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deledateBtn.frame = CGRectMake((WIDTH - 35)/2, CGRectGetMaxY(image.frame)+20, 35, 35);
    [deledateBtn setBackgroundImage:ImageName(@"icon_window_close") forState:UIControlStateNormal];
    [deledateBtn addTarget:self action:@selector(deledateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [alertBgView addSubview:deledateBtn];
    [window addSubview:alertBgView];
}

-(void)deledateBtnClick {
    
    [alertBgView removeFromSuperview];
    
}
//抽奖
-(void)didalBtnClick{
    
    if (verifTokenAndEnter) {
        BigTurnTableViewController * big = [[BigTurnTableViewController alloc]init];
        big.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:big animated:YES];
    }else {
        LogingViewController * login =[[LogingViewController alloc]init];
        login.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:login animated:YES];
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 120;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1080+160+aotoHeight*4)];
    heardView.backgroundColor =[UIColor whiteColor];
    
    
    NSMutableArray * imageArrray = @[].mutableCopy;
    if (bannerDataArray.count>0) {
        
        if (imageArrray.count>0) {
            
            [imageArrray removeAllObjects];
        }
        
        for (NSDictionary * dic in bannerDataArray) {
            
            [imageArrray addObject:dic[@"src"]];
        }
    }
    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, 160) delegate:self placeholderImage:[UIImage imageNamed:@"img_banner_default"]];
    cycleScrollView.backgroundColor =Color(229, 229, 229);
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView.currentPageDotColor = NavBackColor;
    //    cycleScrollView.imageURLStringsGroup = imageArrray;
    cycleScrollView.localizationImageNamesGroup = imageArrray;
    [heardView addSubview:cycleScrollView];
    cycleScrollView.clickItemOperationBlock = ^(NSInteger index) {
        
        NSDictionary * dic = bannerDataArray[index];
        
        if ([dic[@"go_type"] isEqualToString:@"goods"]) { //商品
            
            if (verifTokenAndEnter) {

                GoodsDetailsViewController * goods =[[GoodsDetailsViewController alloc]init];
                goods.hidesBottomBarWhenPushed = YES;
                goods.goodsId = dic[@"go_id"];
                [self.navigationController pushViewController:goods animated:YES];
            }else {
                
                LogingViewController * login =[[LogingViewController alloc]init];
                login.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:login animated:YES];
            }
        }
        if ([dic[@"go_type"] isEqualToString:@"news"]) { //新闻
            
            JuNengLolViewController * lol =[[JuNengLolViewController alloc]init];
            lol.newsIdStr = dic[@"go_id"];
            lol.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:lol animated:YES];
        }
   
    };
    
    UIView * pointView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(cycleScrollView.frame)+5, WIDTH, 115+60)];
    pointView.backgroundColor =[UIColor whiteColor];
    [heardView addSubview:pointView];
    
    UIImageView * image =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-45, 5, 90, 30)];
    image.image = ImageName(@"icon_home_integral");
    [pointView addSubview:image];
    
    NSArray * title_array = @[@"本年目标",@"历史预留",@"当前积分",@"可用积分"];
    NSArray * pointArray=@[@"0",@"0",@"0",@"0"];
    
    if (dataDic) {
        
        NSString * goal = [NSString stringWithFormat:@"%ld",[dataDic[@"score"][@"goal_score"] integerValue]];
        NSString * history = [NSString stringWithFormat:@"%ld",[dataDic[@"score"][@"history_score"] integerValue]];
        NSString * current = [NSString stringWithFormat:@"%ld",[dataDic[@"score"][@"current_score"] integerValue]];
        NSString * available = [NSString stringWithFormat:@"%ld",[dataDic[@"score"][@"available_score"] integerValue]];
        pointArray=@[goal,history,current,available];
    }
    
    for (int i = 0; i<2; i++) {
        UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0+WIDTH/2*i, 40 , WIDTH/2, 60)];
        
        UILabel * numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, WIDTH/2, 20)];
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.textColor = [UIColor redColor];
        numberLabel.font =[UIFont systemFontOfSize:14];
        numberLabel.text = pointArray[i];
        [view addSubview:numberLabel];
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, WIDTH/2, 20)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = RGB(0x666666);
        titleLabel.font =[UIFont systemFontOfSize:14];
        titleLabel.text = title_array[i];
        [view addSubview:titleLabel];
        
        view.backgroundColor =[UIColor whiteColor];
        [pointView addSubview:view];
        if (i<1) {
            
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH/2-0.5, 25, 0.5, 30)];
            lineView.backgroundColor =RGB(0xe5e5e5);
            [view addSubview:lineView];
        }
        
    }
    
    for (int j=0; j<2; j++) {
        
        UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0+WIDTH/2*j, 100 , WIDTH/2, 60)];
        
        UILabel * numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, WIDTH/2, 20)];
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.textColor = [UIColor redColor];
        numberLabel.font =[UIFont systemFontOfSize:14];
        numberLabel.text = pointArray[j+2];
        [view addSubview:numberLabel];
        
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, WIDTH/2, 20)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = RGB(0x666666);
        titleLabel.font =[UIFont systemFontOfSize:14];
        titleLabel.text = title_array[j+2];
        [view addSubview:titleLabel];
        
        view.backgroundColor =[UIColor whiteColor];
        [pointView addSubview:view];
        if (j<1) {
            
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH/2-0.5, 25, 0.5, 30)];
            lineView.backgroundColor = RGB(0xe5e5e5);
            [view addSubview:lineView];
        }
        
    }
    
    UIView * comeView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(pointView.frame), WIDTH, 180)];
    comeView.backgroundColor =[UIColor whiteColor];
    [heardView addSubview:comeView];
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    lineView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    [comeView addSubview:lineView];
    
    UIView * line_twoView = [[UIView alloc]initWithFrame:CGRectMake( WIDTH/2,0, 0.5, 180)];
    line_twoView.backgroundColor =RGB(0xe5e5e5);
    [comeView addSubview:line_twoView];
    
    
    [self creatLeftLabelFrame:CGRectMake(10, 20, WIDTH/2-20, 20) andFont:[UIFont boldSystemFontOfSize:17] andColor:RGB(0xd0aeff) andText:@"每日签到" onView:comeView];
    [self creatLeftLabelFrame:CGRectMake(10, 43, WIDTH/2-20, 20) andFont:[UIFont systemFontOfSize:14] andColor:RGB(0x808080) andText:@"每日一签" onView:comeView];
    [self creatLeftLabelFrame:CGRectMake(10, 62, WIDTH/2-20, 20) andFont:[UIFont systemFontOfSize:14] andColor:RGB(0x808080) andText:@"积分多多" onView:comeView];
    [self creatLeftLabelFrame:CGRectMake( WIDTH/2+10, 20, WIDTH/2-20, 20) andFont:[UIFont boldSystemFontOfSize:17] andColor:RGB(0xf56148) andText:@"幸运大转盘" onView:comeView];
    [self creatLeftLabelFrame:CGRectMake(WIDTH/2+10, 43, WIDTH/2-20, 20) andFont:[UIFont systemFontOfSize:14] andColor:RGB(0x808080) andText:@"小小积分" onView:comeView];
    [self creatLeftLabelFrame:CGRectMake(WIDTH/2+10, 62, WIDTH/2-20, 20) andFont:[UIFont systemFontOfSize:14] andColor:RGB(0x808080) andText:@"大大回馈" onView:comeView];
    
    UIImageView * signImage =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-100, 80, 90, 90)];
    signImage.image = ImageName(@"icon_home_sign");
    [signImage tapAction:^{
        LogingViewController * loging = [[LogingViewController alloc]init];
        loging.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loging animated:YES];
    }];
    
    [comeView addSubview:signImage];
    
    UIImageView * didalImage =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH-100, 80, 90, 90)];
    didalImage.image = ImageName(@"icon_home_dial");
    [comeView addSubview:didalImage];
    
    UIButton * signBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    signBtn.frame = CGRectMake(0, 0, WIDTH/2, 180);
    signBtn.backgroundColor = [UIColor clearColor];
    [signBtn addTarget:self action:@selector(signBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [comeView addSubview:signBtn];
    
    
    UIButton * didalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    didalBtn.frame = CGRectMake(WIDTH/2, 0, WIDTH/2, 180);
    didalBtn.backgroundColor = [UIColor clearColor];
    [didalBtn addTarget:self action:@selector(didalBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [comeView addSubview:didalBtn];
    
    UIView * oneTitView = [self creatTitleViewFrame:CGRectMake(0, CGRectGetMaxY(comeView.frame), WIDTH, 40) andBageColor:RGB(0xc30121) ImageName:@"icon_home_new" andTitle:@"新品首发" andSel:@selector(SeniorBtn:) onView:heardView];
    
    UIScrollView * scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(oneTitView.frame)+10, WIDTH, 170)];
    [heardView addSubview:scrollView];
    //边界弹的效果，默认yes
    scrollView.bounces = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = NO;
    scrollView.delegate = self;
    scrollView.userInteractionEnabled = YES;
    
    
    UIView * btnView;
    
    for (int i =0; i<newsGoodsarray.count; i++) {
        
        NSDictionary * dic = newsGoodsarray[i];
        btnView = [self creatBtnViewFrame:CGRectMake(10 +(15)*i +160*i, 0, 160, 170) andBorderColor:[UIColor clearColor] onView:scrollView];
        
        UIImageView * heardImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160,130)];
        [heardImage sd_setImageWithURL:ImageUrl(dic[@"src"]) placeholderImage:ImageName(@"img_new_default")];
        
        [btnView addSubview:heardImage];
        
        
        [self creatLaberFrame:CGRectMake(5,138, 160, 20) andFontOfSize:14 andTextColor:RGB(0x4c4c4c) andText:dic[@"goods_name"]andAlignment:NSTextAlignmentLeft onView:btnView];
        
        
        UIButton  * goodBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        goodBtn.frame =  CGRectMake(0, 0, 160, 170);
        goodBtn.tag = 100+i;
        [goodBtn addTarget:self action:@selector(goodBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:goodBtn];
        
    }
    
    scrollView.contentSize = CGSizeMake(CGRectGetMaxX(btnView.frame)+10 ,170);
    
    UIView * brandTitView = [self creatTitleViewFrame:CGRectMake(0, CGRectGetMaxY(scrollView.frame), WIDTH, 40) andBageColor:RGB(0xc19a7b6) ImageName:@"icon_home_brand" andTitle:@"品牌头条" andSel:@selector(SeniorBtn:) onView:heardView];
    
    UIView * brandBageView;
    
    float brandWidth = (WIDTH -40)/3;
    float brandHeight = (WIDTH -40)/3 * 1.25;
    
    
    
    
    for (int g = 0; g<3; g++) {
        
        
        brandBageView = [self creatBtnViewFrame:CGRectMake(10+brandWidth*g+10*g, CGRectGetMaxY(brandTitView.frame)+10, brandWidth, brandHeight) andBorderColor:[UIColor clearColor] onView:heardView];
        brandBageView.layer.cornerRadius = 13.f;
        brandBageView.layer.masksToBounds = YES;
        brandBageView.layer.borderWidth = 0.5;
        [brandBageView tapAction:^{
            
            if (verifTokenAndEnter) {
                BrandTitleViewController * brand =[[BrandTitleViewController alloc]init];
                if (g==0) {
                    brand.titleStr = dataDic[@"head"][@"hot"][@"name"];
                    brand.typeStr = @"hot";
                }else if (g==1){
                    brand.typeStr = @"perfect";
                    brand.titleStr = dataDic[@"head"][@"perfect"][@"name"];
                }else {
                    brand.titleStr = dataDic[@"head"][@"big"][@"name"];
                    brand.typeStr = @"big";
                }
                brand.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:brand animated:YES];
            }else {
                
                LogingViewController * login =[[LogingViewController alloc]init];
                login.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:login animated:YES];
            }
        }];
        brandBageView.layer.borderColor = RGB(0xe5e5e5).CGColor;
        NSArray * brandIamegArray;
        NSArray * brandArray;
        if (dataDic) {
            brandIamegArray  = @[dataDic[@"head"][@"hot"][@"img_src"],dataDic[@"head"][@"perfect"][@"img_src"],dataDic[@"head"][@"big"][@"img_src"]];
            
            brandArray = @[dataDic[@"head"][@"hot"][@"name"],dataDic[@"head"][@"perfect"][@"name"],dataDic[@"head"][@"big"][@"name"]];
        }else{
            brandIamegArray = @[@"",@"",@""];
            brandArray =  @[@"",@"",@""];
        }
        
        UIImageView * heardImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,brandWidth ,brandWidth- 10)];
        [heardImage sd_setImageWithURL:ImageUrl(brandIamegArray[g]) placeholderImage:ImageName(@"img_new_default")];
        
        [brandBageView addSubview:heardImage];
        
        
        [self creatLaberFrame:CGRectMake(0,CGRectGetMaxY(heardImage.frame)+10, brandWidth, 20) andFontOfSize:14 andTextColor:RGB(0x4c4c4c) andText:brandArray[g]  andAlignment:NSTextAlignmentCenter onView:brandBageView];
    }
    
    
    // 大咖奖励
    UIView * rankTitView = [self creatTitleViewFrame:CGRectMake(0, CGRectGetMaxY(brandBageView.frame)+10, WIDTH, 40) andBageColor:RGB(0xc9153ed) ImageName:@"icon_home_ranking" andTitle:@"大咖奖励排行榜" andSel:@selector(SeniorBtn:) onView:heardView];
    
    UIView * rankdBageView;
    
    NSArray * randImageArray;
    NSArray * randArray;
    if (dataDic) {
        randImageArray  = @[dataDic[@"award"][@"match"][@"img_src"],dataDic[@"award"][@"display"][@"img_src"],dataDic[@"award"][@"sale"][@"img_src"]];
        
        randArray = @[dataDic[@"award"][@"match"][@"name"],dataDic[@"award"][@"display"][@"name"],dataDic[@"award"][@"sale"][@"name"]];
    }else{
        randImageArray = @[@"",@"",@""];
        randArray =  @[@"",@"",@""];
    }
    
    
    
    
    for (int h = 0; h<3; h++) {
        
        
        rankdBageView = [self creatBtnViewFrame:CGRectMake(10+brandWidth*h+10*h, CGRectGetMaxY(rankTitView.frame)+10, brandWidth, brandHeight) andBorderColor:[UIColor clearColor] onView:heardView];
        rankdBageView.layer.cornerRadius = 13.f;
        rankdBageView.layer.masksToBounds = YES;
        rankdBageView.layer.borderWidth = 0.5;
        rankdBageView.layer.borderColor = RGB(0xe5e5e5).CGColor;
        [rankdBageView tapAction:^{
            
            if (verifTokenAndEnter) {
                
                BagRankListViewController * rank  = [[BagRankListViewController alloc]init];
                rank.hidesBottomBarWhenPushed = YES;
                if (h==0) {
                    //                CompetitionListViewController * compet = [[CompetitionListViewController alloc]init];
                    //                compet.hidesBottomBarWhenPushed = YES;
                    //                [self.navigationController pushViewController:compet animated:YES];
                    rank.titleStr = @"竞赛排名";
                    rank.typeStr = @"ranking";
                    
                }else if (h==1){
                    
                    //                BagRankListViewController * rank  = [[BagRankListViewController alloc]init];
                    //                rank.hidesBottomBarWhenPushed = YES;
                    rank.titleStr = @"陈列大奖";
                    rank.typeStr = @"display";
                    
                }else {
                    rank.titleStr = @"超值促销";
                    rank.typeStr = @"promotion";
                    //                SalesRankViewController * sales = [[SalesRankViewController alloc]init];
                    //                sales.hidesBottomBarWhenPushed = YES;
                    //                [self.navigationController pushViewController:sales animated:YES];
                }
                
                [self.navigationController pushViewController:rank animated:YES];
                
                
            }else {
                LogingViewController * login =[[LogingViewController alloc]init];
                login.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:login animated:YES];
            }
            
        }];
        
        
        UIImageView * heardImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,brandWidth ,brandWidth- 10)];
        [heardImage sd_setImageWithURL:ImageUrl(randImageArray[h]) placeholderImage:ImageName(@"img_new_default")];
        
        [rankdBageView addSubview:heardImage];
        
        
        [self creatLaberFrame:CGRectMake(0,CGRectGetMaxY(heardImage.frame)+10, brandWidth, 20) andFontOfSize:14 andTextColor:RGB(0x4c4c4c) andText:randArray[h]andAlignment:NSTextAlignmentCenter onView:rankdBageView];
    }
    
    // 大咖奖励
    UIView * carTitView = [self creatTitleViewFrame:CGRectMake(0, CGRectGetMaxY(rankdBageView.frame)+10, WIDTH, 40) andBageColor:RGB(0xe97652) ImageName:@"icon_home_sales" andTitle:@"闪购直通车" andSel:@selector(SeniorBtn:) onView:heardView];
    
    UIScrollView * carScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(carTitView.frame)+10, WIDTH, 160)];
    [heardView addSubview:carScrollView];
    //边界弹的效果，默认yes
    carScrollView.bounces = NO;
    carScrollView.backgroundColor = [UIColor whiteColor];
    carScrollView.showsHorizontalScrollIndicator = NO;
    carScrollView.showsVerticalScrollIndicator = NO;
    carScrollView.pagingEnabled = NO;
    carScrollView.delegate = self;
    carScrollView.userInteractionEnabled = YES;
    
    UIView * carBageView;
    
    
    NSArray * carBageArray;
    
    if (dataDic) {
        
        if ([dataDic[@"companys"] isKindOfClass:[NSArray class]]) {
            carBageArray = dataDic[@"companys"];
        }
        
    }
    
    for (int i =0; i<carBageArray.count; i++) {
        
        NSDictionary * dic = carBageArray[i];
        
        carBageView = [self creatBtnViewFrame:CGRectMake(10 +(10)*i +130*i, 0, 130, 160) andBorderColor:[UIColor clearColor] onView:carScrollView];
        carBageView.layer.cornerRadius = 13.f;
        carBageView.layer.masksToBounds = YES;
        carBageView.layer.borderWidth = 0.5;
        carBageView.layer.borderColor = RGB(0xe5e5e5).CGColor;
        [carBageView tapAction:^{
            
            if (verifTokenAndEnter) {
                
                [self  fastGoodsTitleArrayRequestFor:dic[@"company_id"]];
            }else {
                LogingViewController * login =[[LogingViewController alloc]init];
                login.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:login animated:YES];
            }
            
            
            
        }];
        
        UIImageView * heardImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 130,120)];
        [heardImage sd_setImageWithURL:ImageUrl(dic[@"img_src"]) placeholderImage:ImageName(@"img_new_default")];
        
        [carBageView addSubview:heardImage];
        
        
        [self creatLaberFrame:CGRectMake(0,130, 130, 20) andFontOfSize:14 andTextColor:RGB(0x4c4c4c) andText:dic[@"company_name"] andAlignment:NSTextAlignmentCenter onView:carBageView];
        
    }
    
    carScrollView.contentSize = CGSizeMake(CGRectGetMaxX(carBageView.frame)+10 ,160);
    
    
    UIView * foodTitView = [self creatTitleViewFrame:CGRectMake(0, CGRectGetMaxY(carScrollView.frame)+10, WIDTH, 40) andBageColor:RGB(0xa9d12e) ImageName:@"icon_home_cate" andTitle:@"鲫时鱼美食厨房" andSel:@selector(SeniorBtn:) onView:heardView];
    
    NSArray * fishImageArray;
    NSArray * fishArray;
    
    if (dataDic) {
        fishImageArray  = @[dataDic[@"food"][@"chuancai"][@"img_src"],dataDic[@"food"][@"tech"][@"img_src"],dataDic[@"food"][@"life"][@"img_src"]];
        
        fishArray = @[dataDic[@"food"][@"chuancai"][@"name"],dataDic[@"food"][@"tech"][@"name"],dataDic[@"food"][@"life"][@"name"]];
    }else{
        fishImageArray = @[@"",@"",@""];
        fishArray =  @[@"",@"",@""];
    }
    
    
    UIView * foodBageView;
    
    
    for (int h = 0; h<3; h++) {
        
        
        foodBageView = [self creatBtnViewFrame:CGRectMake(10+brandWidth*h+10*h, CGRectGetMaxY(foodTitView.frame)+10, brandWidth, brandHeight) andBorderColor:[UIColor clearColor] onView:heardView];
        foodBageView.layer.cornerRadius = 13.f;
        foodBageView.layer.masksToBounds = YES;
        foodBageView.layer.borderWidth = 0.5;
        foodBageView.layer.borderColor = RGB(0xe5e5e5).CGColor;
        [foodBageView tapAction:^{
            
            NowFishViewController * fishVc = [[NowFishViewController alloc]init];
            fishVc.hidesBottomBarWhenPushed = YES;
            if (h==0) {
                
                fishVc.newsType = @"chuancai";
            }else if (h==1){
                fishVc.newsType = @"tech";
                NSLog(@"第二个");
            }else {
                fishVc.newsType = @"life";
                NSLog(@"第三个");
            }
            fishVc.titleStr = fishArray[h];
            [self.navigationController pushViewController:fishVc animated:YES];
            
        }];
        
        UIImageView * heardImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,brandWidth ,brandWidth- 10)];
        [heardImage sd_setImageWithURL:ImageUrl(fishImageArray[h]) placeholderImage:ImageName(@"img_new_default")];
        
        [foodBageView addSubview:heardImage];
        
        [self creatLaberFrame:CGRectMake(0,CGRectGetMaxY(heardImage.frame)+10, brandWidth, 20) andFontOfSize:14 andTextColor:RGB(0x4c4c4c) andText:fishArray[h]  andAlignment:NSTextAlignmentCenter onView:foodBageView];
    }
    
    
    UIView * lolTitView = [self creatTitleViewFrame:CGRectMake(0, CGRectGetMaxY(foodBageView.frame)+10, WIDTH, 40) andBageColor:RGB(0xe9538b) ImageName:@"icon_home_shaped" andTitle:@"聚能联盟" andSel:@selector(SeniorBtn:) onView:heardView];
    
    NSArray * lolDataArray;
    
    
    if (dataDic) {
        
        lolDataArray = dataDic[@"unionNews"];
    }
    
    UIView * lolBageView;
    for (int h = 0; h<lolDataArray.count; h++) {
        
        NSDictionary * dic = lolDataArray[h];
        
        lolBageView = [self creatBtnViewFrame:CGRectMake(10+brandWidth*h+10*h, CGRectGetMaxY(lolTitView.frame)+10, brandWidth, brandHeight) andBorderColor:[UIColor clearColor] onView:heardView];
        [lolBageView tapAction:^{
            
            JuNengLolViewController * lol =[[JuNengLolViewController alloc]init];
            lol.newsIdStr = dic[@"news_id"];
            lol.titleStr = @"聚能联盟";
            lol.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:lol animated:YES];
            
            if (h==0) {
                NSLog(@"第一个");
            }else if (h==1){
                NSLog(@"第二个");
            }else {
                NSLog(@"第三个");
            }
        }];
        UIImageView * heardImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,brandWidth ,brandWidth)];
        [heardImage sd_setImageWithURL:ImageUrl(dic[@"img_src"]) placeholderImage:ImageName(@"img_new_default")];
        
        [lolBageView addSubview:heardImage];
        
        
        [self creatLaberFrame:CGRectMake(0,CGRectGetMaxY(heardImage.frame)+10, brandWidth, 20) andFontOfSize:14 andTextColor:RGB(0x4c4c4c) andText:dic[@"title"]  andAlignment:NSTextAlignmentCenter onView:lolBageView];
    }
    
    UIView * bagTitView = [self creatTitleViewFrame:CGRectMake(0, CGRectGetMaxY(lolBageView.frame)+10, WIDTH, 40) andBageColor:RGB(0xffb64d) ImageName:@"icon_home_catalogue" andTitle:@"大事件" andSel:@selector(moreNewsBtn:) onView:heardView];
    
    UIImageView * moreImage =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH -50, 0, 40, 40)];
    moreImage.image = ImageName(@"icon_home_more");
    [bagTitView addSubview:moreImage];
    
    return heardView;
}

-(void)creatLeftLabelFrame:(CGRect)rec andFont:(UIFont*)font andColor:(UIColor *)col andText:(NSString *)text onView:(UIView *)view {
    
    UILabel * label =[[UILabel alloc]initWithFrame:rec];
    label.font = font;
    label.textColor =col;
    label.text = text;
    [view addSubview:label];
}
-(void)moreNewsBtn:(UIButton *)btn {
    
    NSLog(@"更多大事件按钮");
    
    MoreNewsViewController * more =[[MoreNewsViewController alloc]init];
    more.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:more animated:YES];
    
}

-(void)goodBtnClick:(UIButton *)button {
    
    if (verifTokenAndEnter) {
        
        
        NSDictionary * dic = newsGoodsarray[button.tag-100];
        
        GoodsDetailsViewController * goods =[[GoodsDetailsViewController alloc]init];
        goods.hidesBottomBarWhenPushed = YES;
        goods.goodsId = dic[@"goods_id"];
        [self.navigationController pushViewController:goods animated:YES];
    }else {
        
        LogingViewController * login =[[LogingViewController alloc]init];
        login.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:login animated:YES];
    }
    
    
}

-(void)SeniorBtn:(UIButton *)btn {
    
    NSLog(@"资深专家");
}



-(UIView *)creatTitleViewFrame:(CGRect)viewRec andBageColor:(UIColor *)col ImageName:(NSString *)imageName andTitle:(NSString *)title andSel:(SEL)sel onView:(UIView *)bgView{
    
    UIView * view =[[UIView alloc]initWithFrame:viewRec];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView * labelView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    labelView.backgroundColor =col;
    
    [view addSubview:labelView];
    
    UIImageView * image =[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 30, 30)];
    image.image =[UIImage imageNamed:imageName];
    [labelView addSubview:image];
    
    UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(40, 10, 120, 20)];
    label.text = title;
    label.textColor =[UIColor whiteColor];
    label.font =[UIFont systemFontOfSize:16];
    [labelView addSubview:label];
    
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, WIDTH, 40);
    button.backgroundColor =[UIColor clearColor];
    [button addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    
    [labelView addSubview:button];
    
    [bgView addSubview:view];
    
    return view;
}



-(UIView *)creatBtnViewFrame:(CGRect)rec andBorderColor:(UIColor *)col onView:(UIView *)view {
    
    UIView * btnView =[[UIView alloc]init];
    btnView.frame = rec;
    btnView.layer.borderWidth = 0.7f;
    btnView.layer.borderColor = col.CGColor;
    btnView.backgroundColor =[UIColor whiteColor];
    //    btnView.layer.cornerRadius = 5.f;
    //    btnView.layer.masksToBounds =YES;
    [view addSubview:btnView];
    
    return btnView;
}

-(void)creatLaberFrame:(CGRect)rec andFontOfSize:(float)size andTextColor:(UIColor*)col andText:(NSString *)text andAlignment:(NSTextAlignment )ment onView:(UIView *)view {
    
    UILabel * label =[[UILabel alloc]initWithFrame:rec];
    label.font = [UIFont systemFontOfSize:size];
    label.textColor =col;
    label.textAlignment = ment;
    label.text = text;
    [view addSubview:label];
    
}

#pragma mark  网络请求部分

-(void)getBannerNetWorking {
    
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,get_banner_api];
    NSDictionary * dic = @{@"type":@"App"};
    
    SHuiMBhubNoTimeLoding;
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                
                bannerDataArray = responseObject[@"datas"];
            }
            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
        
    }];
}

//签到接口
-(void)siginScoreRequest{
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,get_SiginScore_api];
    NSDictionary * dic = @{@"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token")};
    //    SHuiMBhubNoTimeLoding;
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            [self signBtnClickForStr:[NSString stringWithFormat:@"获得%@积分",responseObject[@"datas"][@"score"]]];
            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            
            if ([responseObject[@"code"] intValue]==220) {
                USER_DEFAULTSet(@"", @"token");
                USER_DEFAULTSet(@"", @"enter");
                USER_DEFAULTSet(@"", @"tokenVerify");
                USER_DEFAULTSet(@"", @"user_id");
                USER_DEFAULTSet(@"", @"phone");
            }
        }
        
        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
        
    }];
    
}

-(void)getMainNewsGoodsRequest{
    
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,main_newsGoods_api];
    
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:nil  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                
                newsGoodsarray = responseObject[@"datas"];
            }
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
        
    }];
}

-(void)getMainClassDataRequest{
    
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,main_dataClass_api];
    
    NSDictionary * dic;
    if (verifTokenAndEnter) {
        
        dic=@{@"user_id":USER_DEFAULTGet(@"user_id")};
    }else {
        dic=@{@"user_id":@""};
    }
    
    
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            
            dataDic = responseObject[@"datas"];
            
            if ([dataDic[@"bigNews"] isKindOfClass:[NSArray class]]) {
                newsArray = dataDic[@"bigNews"];
            }
            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
        
    }];
}

-(void)fastGoodsTitleArrayRequestFor:(NSString *)companyId{
    
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,get_fastGoodsList_api];
    NSDictionary * dic = @{@"company_id":companyId,
                           @"page":@"1",
                           @"page_num":@"10"};
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:dic  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([responseObject[@"datas"][@"sorts"] isKindOfClass:[NSArray class]]) {
                
                FlashTrainViewController * flash = [[FlashTrainViewController   alloc]init];
                flash.hidesBottomBarWhenPushed = YES;
                flash.titleStr = responseObject[@"datas"][@"company_name"];
                flash.titleArray = responseObject[@"datas"][@"sorts"];
                flash.company_id = companyId;
                [self.navigationController pushViewController:flash animated:YES];
            }
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
        
    }];
}

-(void)getUserMessageRequest{
    
    NSDictionary * dic = @{@"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token")};
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_userMessage_api) andparameters:dic Success:^(id responseObject) {
        
        if (codeSucess) {
            
        }else {
            
            if ([responseObject[@"code"] intValue]==220) {
                
                USER_DEFAULTSet(@"", @"token");
                USER_DEFAULTSet(@"", @"enter");
                USER_DEFAULTSet(@"", @"tokenVerify");
                USER_DEFAULTSet(@"", @"user_id");
                USER_DEFAULTSet(@"", @"phone");
                           }
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
      
        
    } failed:^(NSError *error) {
        
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
    
}


@end
